package co.fitstart.weapp.api.dao;

import java.util.Date;
import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;

import co.fitstart.entity.order.Order;
import co.fitstart.entity.order.OrderStatus;
import co.fitstart.entity.order.OrderType;
import co.fitstart.entity.order.PaymentStatus;
import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.User;
import co.fitstart.entity.user.agent.Agent;

@Dao("hibernate")
public interface OrderDao {
    
    /**
     * 
     * @param orderType
     * @param agent
     * @param status
     * @return
     */
    @Query("FROM Order WHERE disabled = false AND orderType = :orderType AND agent IS NOT NULL AND agent = :agent AND paymentStatus = :status")
    public List<Order> list(@Variable("orderType") OrderType orderType, @Variable("agent") Agent agent, @Variable("status") PaymentStatus status);

    /**
     * 
     * @param user
     * @param paymentStatus
     * @return
     */
    @Query("FROM Order order WHERE order.disabled = false AND order.user = :user AND order.paymentStatus = :paymentStatus ORDER BY order.payTime DESC, order.createTime DESC")
    public List<Order> list(@Variable("user") User user, @Variable("paymentStatus") PaymentStatus paymentStatus);
    
    /**
     * 
     * @param couponIds
     * @return
     */
    @Query("SELECT order FROM OrderAttribute WHERE name = 'couponId' AND value IN ( :couponIds)")
    public List<Order> list(@Variable("couponIds") List<String> couponIds);
    
    /**
     * 
     * @param orderStatus
     * @param orderType
     * @param paymentStatus
     * @param payTime
     * @param student
     * @return
     */
    @Query("SELECT SUM(paidPrice) FROM Order WHERE disabled = false AND orderStatus IN (:orderStatus) AND orderType IN (:orderType) AND course.experience = false AND paymentStatus = :paymentStatus AND payTime > :payTime AND student = :student")
    public Long sumOfflinePaid(@Variable("orderStatus") List<OrderStatus> orderStatus, @Variable("orderType") List<OrderType> orderType, @Variable("paymentStatus") PaymentStatus paymentStatus, @Variable("payTime") Date payTime, @Variable("student") Student student);
}
